package com.dayuan.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.dayuan.bean.BackUser;
import com.dayuan.bean.MerchantInfo;
import com.dayuan.service.MerchantInfoService;
import com.dayuan.utils.SecurityUtil;
import com.dayuan.utils.pageHelper;

@Controller
@RequestMapping("/Merchant")
public class MerchantController {

	@Resource
	private MerchantInfoService merchantInfoService;

	/**
	 * 查询商家信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getInfo.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView getInfo() {
		// BackUser backUser = SecurityUtil.getBackUser();
		// MerchantInfo items =
		// merchantInfoService.selectByUid(backUser.getId());
		MerchantInfo items = merchantInfoService.selectByUid(3);

		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("item", items);
		modelAndView.setViewName("MerchantShow");
		return modelAndView;
	}

	// 管理员查询得到商家信息
	@RequestMapping(value = "/getMerchantInfo.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView getMerchantInfo(Integer currentPageIndex, ModelAndView modelAndView, String shopName) {
		try {
			if (currentPageIndex == null) {
				currentPageIndex = 1;
			}
			int totalCount = 0;
			pageHelper page = null;
			List<Integer> pageNumList = null;
			List<MerchantInfo> list = null;
			if (shopName == null) {
				// 查出全部商家
				totalCount = merchantInfoService.selectCount(null);

				page = new pageHelper(currentPageIndex, totalCount);
				pageNumList = page.getPageNumList();
				list = merchantInfoService.selectMerchantByLimit(page.getStartIndex(), page.getPageNum());
			} else {
				// 按商家名查询
				MerchantInfo merchantInfo = new MerchantInfo();
				merchantInfo.setShopName(shopName);
				totalCount = merchantInfoService.selectCount(merchantInfo);
				page = new pageHelper(currentPageIndex, totalCount);
				pageNumList = page.getPageNumList();
				list = merchantInfoService.selectMerchantByParam(merchantInfo, page.getStartIndex(), page.getPageNum());
			}
			modelAndView.addObject("totalCount", totalCount);
			modelAndView.addObject("page", page);
			modelAndView.addObject("pageNumList", pageNumList);
			modelAndView.addObject("merchantList", list);
			modelAndView.setViewName("merchantManege");
			return modelAndView;
		} catch (Exception e) {
			modelAndView.addObject("msg", "网络错误！");
			modelAndView.setViewName("merchantManege");
			return modelAndView;
		}
	}

	/**
	 * 跟新商家信息
	 * 
	 * @param merchantInfo
	 * @return
	 */
	@RequestMapping(value = "/updateMerchant.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView editMerchant(MerchantInfo merchantInfo) {
		merchantInfoService.updateMerchanInfo(merchantInfo);
		MerchantInfo items = merchantInfoService.selectByPrimaryKey(merchantInfo.getId());
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("item", items);
		modelAndView.setViewName("MerchantShow");
		return modelAndView;
	}

	/**
	 * 插入信息
	 */
	@RequestMapping(value = "/insterMerchant.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public ModelAndView insterMerchant(MerchantInfo merchantInfo, MultipartFile license, HttpSession session) {
		MerchantInfo mc = new MerchantInfo();
		mc.setCashDeposit(merchantInfo.getCashDeposit());
		mc.setShipAddress(merchantInfo.getShipAddress());
		mc.setShopkeeper(merchantInfo.getShopkeeper());
		mc.setShopName(merchantInfo.getShopName());

		String pic_path = session.getServletContext().getRealPath("/") + "img";

		String originalFileName = license.getOriginalFilename();
		String newFileName = UUID.randomUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
		File newFile = new File(pic_path, newFileName);
		if (!newFile.exists()) {
			newFile.getParentFile().mkdirs();
		}
		try {
			license.transferTo(newFile);
		} catch (IOException e) {
			e.printStackTrace();
		}
		mc.setLicenseUrl(newFileName);
		// BackUser backUser= SecurityUtil.getBackUser();
		// mc.setuId(backUser.getId());
		mc.setuId(4);
		merchantInfoService.insterMerchanInfo(mc);
		MerchantInfo items = merchantInfoService.selectByPrimaryKey(mc.getId());
		ModelAndView modelAndView = new ModelAndView();
		modelAndView.addObject("item", items);
		modelAndView.setViewName("MerchantShow");
		return modelAndView;
	}

	/**
	 * 商家完善信息
	 */
	@RequestMapping(value = "/getPage.shtml", method = { RequestMethod.POST, RequestMethod.GET })
	public String getPage() {
		
		return "MerchantInformation";
	}
}
